package Common;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Random;

import javax.microedition.lcdui.Image;
import javax.microedition.rms.RecordStore;


public final class CRes
{
	public static FrameImage imgTile;

	private static Random rnd;
	private static int random_num[],random_i;
	public static void init() {
		rnd=new Random();
		random_num=new int[100];
		for (int i=0;i<100;i++)
			random_num[i]=rnd.nextInt();
		
		cos=new short[91];
		tan=new int[91];
		for (int i=0;i<=90;i++)
		{
			cos[i]=sin[90-i];
			if (cos[i]==0) tan[i]=Integer.MAX_VALUE;
			else tan[i]=(sin[i]<<10)/cos[i];
		}
	}
	public static final void set(int value,int off,byte[] data)
	 {
	  data[off]=(byte)((value>>24) & 0xff);
	  data[off+1]=(byte)((value>>16) & 0xff);
	  data[off+2]=(byte)((value>>8) & 0xff);
	  data[off+3]=(byte)(value & 0xff);
	 }
	 
	 public static final void set(String st,int len,int off,byte[] data)
	 {
	  byte[] b=st.getBytes();
	  // gan cac ky tu vo mang
	  for (int i=0;i<len && i<b.length;i++)
	   data[off+i]=b[i];
	  // gan cac ky tu con lai la khoang trang
	  for (int i=b.length;i<len;i++)
	   data[off+i]=32;
	 }
	 
	 public static final int getInt(int off,byte[] data)
	 {
	  return (byte2int(data[off])<<24) | (byte2int(data[off+1])<<16)
	  | (byte2int(data[off+2])<<8) | byte2int(data[off+3]);
	 }
	 
	 public static final int getShort(int off,byte[] data)
	 {
	  return (byte2int(data[off])<<8) | byte2int(data[off+1]);
	 }
	 
	 public static final String getString(int off,int len,byte[] data)
	 {
		 return new String(data,off,len).trim();
	 }
	 
	 //xy ly chuyen tu byte to int
	 public static final int byte2int(byte b)
	 {
		 return b & 0xff;
	 }
	 	
	// ham luu RMSdata
	public static final void saveRMSData(byte[] data,String name)
	{
		try
		{
			RecordStore rs = RecordStore.openRecordStore(name,true);
			if(rs.getNumRecords() == 0) rs.addRecord(data,0,data.length);
			else rs.setRecord(1,data,0,data.length);
			rs.closeRecordStore();
		}
		catch(Exception ex)
		{
				ex.printStackTrace();
		}
	}

	// ham load  RMSdata
	public static final byte[] loadRMSData(String name)
	{
		try
			{
				RecordStore rs = RecordStore.openRecordStore(name,false);
				byte[] data = rs.getRecord(1);
				rs.closeRecordStore();
				return data; 
			 }
			 catch(Exception ex)
			 {
				// ex.printStackTrace();
				 return null;
			 }
		 }
	//ham xu ly su kien load file
	public static final byte[] loadFile(String fileName)
	{
		try
		{
			InputStream is = "".getClass().getResourceAsStream(fileName);
			ByteArrayOutputStream bytes=new ByteArrayOutputStream();
			int i;
			while ((i=is.read())!=-1)
			{
				bytes.write(i);				
			}
			is.close();
			is = null;
			byte[] b=bytes.toByteArray();
						
			bytes.close();
			return b;
		}
		catch(Exception ex)
		{
			return null;
		}
	}
	
	 private static short sin[] = { 0, 18, 36, 54, 71, 89, 107, 125, 143, 160,
			178, 195, 213, 230, 248, 265, 282, 299, 316, 333, 350, 367, 384,
			400, 416, 433, 449, 465, 481, 496, 512, 527, 543, 558, 573, 587,
			602, 616, 630, 644, 658, 672, 685, 698, 711, 724, 737, 749, 761,
			773, 784, 796, 807, 818, 828, 839, 849, 859, 868, 878, 887, 896,
			904, 912, 920, 928, 935, 943, 949, 956, 962, 968, 974, 979, 984,
			989, 994, 998, 1002, 1005, 1008, 1011, 1014, 1016, 1018, 1020,
			1022, 1023, 1023, 1024, 1024 };
	private static short cos[];
	private static int tan[];

	//tinh sin goc 0-359
	public static final int sin(int a)
	{
		if (a>=0 && a<90) return sin[a];
		if (a>=90 && a<180) return sin[180-a];
		if (a>=180 && a<270) return -sin[a-180];
		return -sin[360-a];
	}

	// tinh cos goc 0-359
	public static final int cos(int a)
	{
		System.out.println("YYYYYYYYYYYYYYYYY");
		if (a>=0 && a<90) return cos[a];
		if (a>=90 && a<180) {
			System.out.println("aaaaaaaaaaa ");
			return -cos[180-a];
		}
		if (a>=180 && a<270) return -cos[a-180];
		return cos[360-a];
	}

	// tinh tan goc 0-359
	public static final int tan(int a)
	{
		if (a>=0 && a<90) return tan[a];
		if (a>=90 && a<180) return -tan[180-a];
		if (a>=180 && a<270) return tan[a-180];
		return -tan[360-a];
	}

	public static final int atan(int a)
	{
		for (int i=0;i<=90;i++)
			if (tan[i]>=a) return i;
		return 0;
	}

	// tinh goc tao boi dx, dy
	public static final int angle(int dx,int dy)
	{
		int angle;
		if (dx!=0)
		{
			int tan=Math.abs((dy<<10)/dx);
			angle=atan(tan);
			if (dy>=0 && dx<0) angle=180-angle;
			if (dy<0 && dx<0) angle=180+angle;
			if (dy<0 && dx>=0) angle=360-angle;
			//System.out.println(a+" "+angle);
		}
		else
		{
			angle=dy>0 ? 90 : 270; 
		}
		return angle;
	}

	// dieu chinh lai goc <0 hoac >360 ve trong khoang 0-360
	public static final int fixangle(int angle)
	{
		if (angle>=360) angle-=360;
		if (angle<0) angle+=360;
		return angle;
	}

	// tinh hieu cua 2 goc
	public static final int subangle(int a1,int a2)
	{
		int a=a2-a1;
		if (a<-180) return a+360;
		if (a>180) return a-360;
		return a;
	}
	
	//tinh binh phuong
	public static int pow(int data, int x) {
		  // TODO Auto-generated method stub
		  int t=1;
		  for(int i=0;i<x;i++){
		   t*=data;
		  }
		  return t;
		 }
	
	//xoay image trong game the goc
	public static Image rotateImage(Image src, int angle, Common.mGraphics g, int x,int y, boolean isRGB)
	{
		  int sw = src.getWidth();
		  int sh = src.getHeight();
		  int[] srcData = new int[sw * sh];
		  src.getRGB(srcData, 0, sw, 0, 0, sw, sh);
		  int[] dstData = new int[sw * sh];
		  // double rads = angle * Math.PI / 180.f;
		  float sa = (float) CRes.sin(angle);
		  float ca = (float) CRes.cos(angle);
		  int isa = (int) (256 * sa) / 1000;
		  int ica = (int) (256 * ca) / 1000;
		  int my = -(sh >> 1);
		  for (int i = 0; i < sh; i++) {
		   int wpos = i * sw;
		   int xacc = my * isa - (sw >> 1)*  ica + ((sw >> 1) << 8);
		   int yacc = my * ica + (sw >> 1) * isa + ((sh >> 1) << 8);
		   for (int j = 0; j < sw; j++) {
		    int srcx = (xacc >> 8);
		    int srcy = (yacc >> 8);
		    if (srcx < 0)
		     srcx = 0;
		    if (srcy < 0)
		     srcy = 0;
		    if (srcx > sw - 1)
		     srcx = sw - 1;
		    if (srcy > sh - 1)
		     srcy = sh - 1;
		    dstData[wpos++] = srcData[srcx + srcy * sw];
		    xacc += ica;
		    yacc -= isa;
		   }
		   my++;
		  }
		  // Image a = Image.createRGBImage(dstData, sw, sh, true);
		  if (isRGB) {
		   g.drawRGB(dstData, 0, sw, x - sw / 2, y - sw / 2, sw, sh, true,false);
		   return null;
		  } else
			 
		   return Image.createRGBImage(dstData, sw, sh, true);
		  // return a;

		 }
}